<?php
class Manager_SectionSolutionController extends Zendvn_Controller_Action{
	private $_arrParam;
	private $_mainAction;
	protected $_currentController;
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	
	public  function init()
	{
	    $this->_arrParam = $this->_request->getParams();	
        $this->loadTemplate(TEMPLATE_PATH . '/admin/system');
        
        $this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam; 
	    

         $ns = new Zend_Session_Namespace('login'); 
	  
	    if(($ns->id)<1) 
	      {
	      $this->_redirect('manager/');
	      }      
	  
	    $tbl_menu = new Manager_Model_menu();
	    $this->view->language=$tbl_menu->get_language();
	}
	
	public function indexAction()
	{
	 
	    $this->view->Title = "Section Solution Manager .";
		$this->view->headTitle($this->view->Title,true);
		
		$tbl_section=new Manager_Model_sectionsolution();
		$result= $tbl_section->list_section();
		
		if($this->_request->getCookie('status')==NULL){ $status=-1; } 
			 else { $status=$this->_request->getCookie('status');}
		
		if($this->_request->getPost('keyword')!=NULL){
			 $keyword=$this->_request->getParam('keyword');
			 $this->view->keyword=$keyword;
             $listArticles = $tbl_section->searchkq($keyword,$status);
          	 $currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
				 $currentPage = $i;
          	 }
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(10)
                    ->setPageRange(5)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		 } 
    	else 
         {
     	 	$listArticles = $tbl_section->listItemabout($status);
          	$currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
          	    $currentPage = $i;
          	}
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(45)
                    ->setPageRange(10)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		}
       
	}
	
	public function statusAction(){
	$db = Zend_Registry::get('connectDb');
    $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi')
       {
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('sections_solution',array('published'=>$status),$where);
			    }
       }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('sections_solution',array('published'=>$status),$where);			
		   	 
	      }
	 $this->_redirect($this->_mainAction);
	 $this->_helper->viewRenderer->setNoRender();
	  }

	public function sortAction(){ 
		$db = Zend_Registry::get('connectDb');
		if($this->_request->isPost()){
		  
		foreach ($this->_arrParam['cid'] as $val){ 
		$where = 'id = ' . $val;
		$db->update('sections',array('order'=>$this->_arrParam['order'][$val]),$where);
		
		}
		    $this->_redirect($this->_mainAction);
		    $this->_helper->viewRenderer->setNoRender();
		} 
      }
	
    public function addAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Section Manager [New].";
	  $this->view->headTitle($this->view->Title,true);
	  $tbl_section              = new Manager_Model_sectionsolution();
	  
	 if($this->_request->isPost()==true)
		{	
		   $data               = $this->_arrParam;
		   $validate           = new Zendvn_Validate_check();
		   $error              = $validate->check_empty($data['title']);
		   $this->view->error  = $error;
		   
		   if ($data['language_id']==0)
	         {
	           $error="Select language.";
	           $this->view->error=$error;
	           
	          }
		   
		   if (empty($error)==true)
		    {
		     $tbl_section->save($data);	
	         $this->_redirect('manager/section-solution');	
		    }
		}
		
	}
	
	public function editAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Section Manager [Edit].";
	  $this->view->headTitle($this->view->Title,true);
	  
	  $tbl_section = new Manager_Model_sectionsolution();
	  $id=$this->_request->getParam('id');
	  $result=$tbl_section->getrow($id);
	  $this->view->data=$result;
	  
	  if ($this->_request->isPost())  
		  {
		  	$data=$this->_arrParam;
			$tbl_section->save($data);	
			$this->_redirect('manager/section-solution');
		  } 
    
	}

	public  function deleteAction()
	{
		$res = new Manager_Model_sectionsolution();
		$id=$this->_request->getParam('id');
		$res->delete_data($id);
		$this->_redirect('manager/section');
	}
	
    public function mutiAction()
	{
	    $tblsection=new Manager_Model_sectionsolution();
		$tblsection->delete_muti($this->_arrParam);
		$this->_redirect('manager/section');
		$this->_helper->viewRenderer->setNoRender();
	}
	
   
}